﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;

using Buddy.BehaviorTree;
using Buddy.Common;
using Buddy.CommonBot;
using Buddy.Swtor;
using Buddy.Swtor.Objects;

using WingIt.Dynamics;
using WingIt.Routines;
using WingIt.Routines.Mirrors;

using Action = Buddy.BehaviorTree.Action;


namespace WingIt
{
    [RevisionControlId("$Id: SpecHandler.cs 699 2012-09-04 19:11:50Z Chinajade $")]
    static class SpecHandler
    {
        public static SkillTreeId PrimarySpec(this TorPlayer player)
        {
            Tuple<SkillTreeId,SkillTreeId,SkillTreeId> skillTrees = player.ClassSkillTrees();
            var pointsInTree1 = player.GetSkillPointsSpentInTree(skillTrees.Item1);
            var pointsInTree2 = player.GetSkillPointsSpentInTree(skillTrees.Item2);
            var pointsInTree3 = player.GetSkillPointsSpentInTree(skillTrees.Item3);

            if (pointsInTree1 > pointsInTree2 && pointsInTree1 > pointsInTree3)
                { return (skillTrees.Item1); }

            else if (pointsInTree2 > pointsInTree1 && pointsInTree2 > pointsInTree3)
                { return (skillTrees.Item2); }

            else if (pointsInTree3 > pointsInTree1 && pointsInTree3 > pointsInTree2)
                { return (skillTrees.Item3); }

            return (SkillTreeId.None);
        }

        public static string TalentBuild(TorPlayer player)
        {
            Tuple<SkillTreeId,SkillTreeId,SkillTreeId>  classSkillTrees = player.ClassSkillTrees();
            string  advancedClassName   = player.AdvancedClass.ToString();
            string  className           = player.Class.ToString();
            int     pointsInTree1       = player.GetSkillPointsSpentInTree(classSkillTrees.Item1);
            int     pointsInTree2       = player.GetSkillPointsSpentInTree(classSkillTrees.Item2);
            int     pointsInTree3       = player.GetSkillPointsSpentInTree(classSkillTrees.Item3);

            if (player.AdvancedClass == AdvancedClass.None)
                { return ("None"); }

            else if ((pointsInTree1 <= 0) && (pointsInTree2 <= 0) && (pointsInTree3 <= 0))
                { return (string.Format("None ({0} is unspec'd)", player.AdvancedClass.ToString())); }

            return (string.Format("{0} {1} | {2} {3} | {4} {5}",
                        pointsInTree1,
                        classSkillTrees.Item1.ToString().Replace(className, "").Replace(advancedClassName, ""),
                        pointsInTree2,
                        classSkillTrees.Item2.ToString().Replace(className, "").Replace(advancedClassName, ""),
                        pointsInTree3,
                        classSkillTrees.Item3.ToString().Replace(className, "").Replace(advancedClassName, "")));
        }

        #region Private/Utility
        private static Tuple<SkillTreeId,SkillTreeId,SkillTreeId> ClassSkillTrees(this TorPlayer player)
        {
            Tuple<SkillTreeId,SkillTreeId,SkillTreeId> classSkillTrees;

            if (SkillTrees.TryGetValue(player.NormalizedClass(), out classSkillTrees))
                { return (classSkillTrees); }

            Logger.MaintenanceError("{0}: No SkillTrees entries for {1}-{2}", Logger.GetMyMethodName(), player.Class, player.AdvancedClass);

            return (Tuple.Create(SkillTreeId.None, SkillTreeId.None, SkillTreeId.None));
        }

        private static Dictionary<CharacterClass, Tuple<SkillTreeId,SkillTreeId,SkillTreeId>> SkillTrees = new Dictionary<CharacterClass, Tuple<SkillTreeId,SkillTreeId,SkillTreeId>>()
        {
            // Basic Classes, Empire
            { CharacterClass.Warrior, Tuple.Create(SkillTreeId.None, SkillTreeId.None, SkillTreeId.None) },
            { CharacterClass.Inquisitor, Tuple.Create(SkillTreeId.None, SkillTreeId.None, SkillTreeId.None) },
            { CharacterClass.Agent, Tuple.Create(SkillTreeId.None, SkillTreeId.None, SkillTreeId.None) },
            { CharacterClass.BountyHunter, Tuple.Create(SkillTreeId.None, SkillTreeId.None, SkillTreeId.None) },

            // Advanced Classes, Empire
            { CharacterClass.Juggernaut, Tuple.Create(SkillTreeId.JuggernautImmortal, SkillTreeId.JuggernautRage, SkillTreeId.JuggernautVengeance) },
            { CharacterClass.Marauder, Tuple.Create(SkillTreeId.MarauderAnnihilation, SkillTreeId.MarauderCarnage, SkillTreeId.MarauderRage) },
            { CharacterClass.Assassin, Tuple.Create(SkillTreeId.AssassinDarkness, SkillTreeId.AssassinDeception, SkillTreeId.AssassinMadness) },
            { CharacterClass.Sorcerer, Tuple.Create(SkillTreeId.SorcererCorruption, SkillTreeId.SorcererLightning, SkillTreeId.SorcererMadness) },
            { CharacterClass.Sniper, Tuple.Create(SkillTreeId.SniperMarksmanship, SkillTreeId.SniperEngineering, SkillTreeId.SniperLethality) },
            { CharacterClass.Operative, Tuple.Create(SkillTreeId.OperativeMedic, SkillTreeId.OperativeConcealment, SkillTreeId.OperativeLethality) },
            { CharacterClass.Mercenary, Tuple.Create(SkillTreeId.MercenaryArsenal, SkillTreeId.MercenaryBodyguard, SkillTreeId.MercenaryFirebug) },
            { CharacterClass.Powertech, Tuple.Create(SkillTreeId.PowertechAdvanced, SkillTreeId.PowertechFirebug, SkillTreeId.PowertechShieldTech) },

            // Basic Classes, Republic
            { CharacterClass.Knight, Tuple.Create(SkillTreeId.None, SkillTreeId.None, SkillTreeId.None) },
            { CharacterClass.Consular, Tuple.Create(SkillTreeId.None, SkillTreeId.None, SkillTreeId.None) },
            { CharacterClass.Smuggler, Tuple.Create(SkillTreeId.None, SkillTreeId.None, SkillTreeId.None) },
            { CharacterClass.Trooper, Tuple.Create(SkillTreeId.None, SkillTreeId.None, SkillTreeId.None) },

            // Advanced Classes, Republic
            { CharacterClass.Guardian, Tuple.Create(SkillTreeId.GuardianDefense, SkillTreeId.GuardianFocus, SkillTreeId.GuardianVigilance) },
            { CharacterClass.Sentinel, Tuple.Create(SkillTreeId.SentinelCombat, SkillTreeId.SentinelFocus, SkillTreeId.SentinelWatchman) },
            { CharacterClass.Shadow, Tuple.Create(SkillTreeId.ShadowCombat, SkillTreeId.ShadowInfiltration, SkillTreeId.ShadowBalance) },
            { CharacterClass.Sage, Tuple.Create(SkillTreeId.SageBalance, SkillTreeId.SageSeer, SkillTreeId.SageTelekinetics) },
            { CharacterClass.Gunslinger, Tuple.Create(SkillTreeId.GunslingerDirtyFighting, SkillTreeId.GunslingerSaboteur, SkillTreeId.GunslingerSharpshooter) },
            { CharacterClass.Scoundrel, Tuple.Create(SkillTreeId.ScoundrelSawbones, SkillTreeId.ScoundrelScrapper, SkillTreeId.ScoundrelDirtyFighting) },
            { CharacterClass.Commando, Tuple.Create(SkillTreeId.CommandoCombatMedic, SkillTreeId.CommandoGunnery, SkillTreeId.CommandoAssaultSpecialist) },
            { CharacterClass.Vanguard, Tuple.Create(SkillTreeId.VanguardAssaultSpecialist, SkillTreeId.VanguardShieldSpecialist, SkillTreeId.VanguardTactics) }
        };
        #endregion
    }     
}
